package pl.edu.mimuw.mas.agent.behaviour.helicopter.areaassignment;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pl.edu.mimuw.mas.agent.HelicopterAgent;
import pl.edu.mimuw.mas.message.PositionMessage;
import pl.edu.mimuw.mas.util.Position;

/**
 * Zachowanie wysyłające liderowi pozycje helikoptera.
 */
public class SendPositionToLeaderBehaviour extends OneShotBehaviour {
	private static final long serialVersionUID = -6120559887334573565L;
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	private AID leaderAID; 
	private Position position;

	public SendPositionToLeaderBehaviour(Agent agent, AID leaderAID, Position position) {
		super(agent);
		this.leaderAID = leaderAID;
		this.position = position;
	}

	public void action() {
		try {
			ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
			msg.addReceiver(leaderAID);		
			msg.setConversationId(HelicopterAgent.HELICOPTER_TELL_POSITION_TO_LEADER_CONV);
			msg.setContentObject(new PositionMessage(myAgent.getAID(), position));
			myAgent.send(msg);
		} catch (IOException e) {
			logger.error("{}: Błąd podczas wysyłania pozycji do agenta", myAgent.getName(), e);
			myAgent.doDelete();
		}
	}
}
